// pagesMine/pages/businessCard/businessCard.js
import ajax from "../../../utils/index"
const canvasfun = require("./createPoster")
const app = getApp()
Page({
  ...canvasfun,
  /**
   * 页面的初始数据
   */
  data: {
    cardId: '',
    tempImage: '',
    modeShow: false
  },
 
  editCardEvent() {
    const { cardDetail } = this.data
    wx.navigateTo({ url: `/pagesMine/pages/businessCardEdit/businessCardEdit?cardId=${cardDetail.id}` })
  },
  authorEvent() {
    this.setData({
      modeShow: !this.data.modeShow
    })
  },
  bindlongtap() {
    wx.saveImageToPhotosAlbum({
      filePath: this.data.tempImage,
      success: (res) => {
        wx.showToast({
          title: '保存成功',
          duration: 1000,
        })
        this.setData({
          modeShow: false
        })
      }
    })
  },
  async create_poster() {
    let query = wx.createSelectorQuery();
    query.select('.mycanvas').boundingClientRect(async (canvasInfo) => {
      const context = wx.createCanvasContext('mycanvas');
      const res = await this.createPoster(this.data.cardDetail, context, canvasInfo);
      this.setData({ tempImage: res })
    }).exec();
  },
  /**
   * 接口 - 查询名片详情
   */
  get_card_detail: function () {
    let that = this
    API.get(`/wx/appletCard/getInfo?id=${this.data.cardId}`).then(res => {
      if (res.code == 200) {
        let cardDetail = res.data
        if (cardDetail.phone) {
          cardDetail.phoneShow = cardDetail.phone.substring(0, 3) + '****' + cardDetail.phone.substring(7, 11)
        }
        let siteIndex = ''
        if (cardDetail.site) {
          siteIndex = cardDetail.site.indexOf('市')
          siteIndex != -1 ? cardDetail.site = cardDetail.site.substr(siteIndex) : ''
        }
        console.log(siteIndex)
        this.setData({
          cardDetail: cardDetail
        })
        this.create_poster()
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    })
  },

  /**
  * 接口 - 查询当前用户名片详情
  */
  get_user_card_detail: function () {
    let that = this
    ajax.get('/wx/appletCard/homeCard').then(res => {
      if (res.code == 200) {
        let cardDetail = res.data
        if (cardDetail.phone) {
          cardDetail.phoneShow = cardDetail.phone.substring(0, 3) + '****' + cardDetail.phone.substring(7, 11)
        }
        let siteIndex = ''
        if (cardDetail.site) {
          siteIndex = cardDetail.site.indexOf('市')
          siteIndex != -1 ? cardDetail.site = cardDetail.site.substr(siteIndex) : ''
        }
        console.log(siteIndex)
        this.setData({
          cardDetail: cardDetail
        })
        this.create_poster()
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    })
  },

  /**
   * 拨打电话
   */
  callPhone: function () {
    if (!this.data.cardDetail.phone) return ajax.showToast("手机号不存在")
    wx.makePhoneCall({
      phoneNumber: this.data.cardDetail.phone,
    })
  },

  /**
   * 添加微信
   */
  addwx: function () {
    // console.log('45453',this.data.cardDetail)
    if (!this.data.cardDetail.wxYard) return ajax.showToast("没有微信")
    wx.previewImage({ current: qrcode, urls: [this.data.cardDetail.wxYard] })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.data.cardId ? this.get_card_detail() : this.get_user_card_detail()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})